﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace TakeoutMangesDAL
{
    public class OrdersDAL
    {
        public static int InsertOrders(Model.Orders OS, out int a)
        {
            string sqlstr = "insert into OrderInfo values(@ProductID,@Isdelete);select @@identity a";
            SqlParameter[] pams = new SqlParameter[]
            {
                new SqlParameter("@ProductID",OS.ProductID),
                new SqlParameter("@Isdelete",OS.Isdelete),
            };
            SqlDataReader reader = DBHelper.ExecuteReader(sqlstr, CommandType.Text, pams);
            int OrderID = 0;
            while (reader.Read())
            {
                OrderID = Convert.ToInt32(reader["a"]);
            }
            a = OrderID;
            return a;
        }
        public static List<OrderInfo> orderInfos(int UserID)
        {
            string sqltr = "select * from Orders where 1=1  ";
            if (UserID > 0) {
                sqltr += " and UserID=@UserID";
            }
            SqlParameter[] paras = new SqlParameter[] {
            new SqlParameter("@UserID",UserID)
            };
            SqlDataReader reader = DBHelper.ExecuteReader(sqltr, CommandType.Text, paras);
            List<OrderInfo> list = new List<OrderInfo>();
            while (reader.Read())
            {
                OrderInfo od = new OrderInfo
                {
                    OrderID = Convert.ToInt32(reader["OrderID"]),
                    OrderDate = Convert.ToDateTime(reader["OrderDate"]),
                    OrderState = Convert.ToInt32(reader["OrderState"]),
                    OrderInfoID = Convert.ToInt32(reader["OrderInfoID"]),
                    UserID = Convert.ToInt32(reader["UserID"]),
                    Remark = reader["Remark"].ToString(),
                };
                list.Add(od);
            }
            reader.Close();
            return list;
        }
        public static int UpadateOrder(int OrderID)
        {
            string sqltr = "delete from Orders where OrderID=@OrderID";
            SqlParameter[] paras = new SqlParameter[] {
            new SqlParameter("@OrderID",OrderID)
            };
            return DBHelper.ExecuteNonQuery(sqltr, paras);
        }
    }
}
